<?php

namespace console\controllers;

use common\models\Classify;
use common\models\Course;
use common\models\CourseCatalog;
use common\models\CourseContent;
use common\utils\Utils;
use Yii;
use common\models\File;

/**
 * 创建课程控制台程序
 * Class FileController
 */
class CreateController extends \yii\console\Controller
{
    /**
     * @name: actionSeeder 填充课程数据
     * @return void
     * @author: 朱晓进  Storm <qhj1989@qq.com>
     */
    public function actionSeeder() {
        $faker = \Faker\Factory::create('zh_CN');
        $a = 0;

        for ($cl = 0; $cl < 2; $cl++) {
            $classify = new Classify();
            $classify->title = $faker->word;
            $classify->parent_id = 0;
            $classify->created_at = $faker->unixTime;
            $classify->updated_at = $faker->unixTime;

            if( !$classify->save() ){
                var_dump($classify->errors);
                break;
            }

            for ($scl = 0; $scl < 1; $scl++) {
                $subclassify = new Classify();
                $subclassify->title = $faker->word;
                $subclassify->parent_id = $classify->id;
                $subclassify->created_at = $faker->unixTime;
                $subclassify->updated_at = $faker->unixTime;

                if( !$subclassify->save() ){
                    var_dump($subclassify->errors);
                    break;
                }
            }
        }

        for ($i = 0; $i < 4; $i++) {
            //创建课程封面
            $file = new File();
            $file->id = Utils::uniqueKey();
            $file->user_id = 1;
            $file->filename = $faker->word;
            $file->size = $faker->numberBetween(1000, 9999) . "";
            $file->hash = $faker->sha1;
            $file->type = 'image/jpg';
            $file->ext = 'jpg';
            $file->server = $faker->imageUrl(476, 270);
            $file->created_at = $faker->unixTime;
            $file->updated_at = $faker->unixTime;

            if (!$file->save()) {
                var_dump($file->errors);
                break;
            }
            //创建课程
            //'id','user_id', 'classify_id', 'speaker_id', 'hour','intro','title'
            $course = new Course();
            $course->id = Utils::uniqueKey();
            $course->title = $faker->word;
            $course->classify_id = 1;
            $course->speaker_id = 1;
            $course->hour = rand(10, 20);
            $course->intro = $faker->paragraph();
            $course->user_id = 1;
            $course->created_at = time();
            $course->updated_at = time() + 724000;
            $course->file_id = $file->id;

            if ($course->save()) {

                $a += 1;

                for ($ct = 0; $ct < 6; $ct++) {
                    $catalog = new CourseCatalog();
                    $catalog->id = Utils::uniqueKey();
                    $catalog->user_id = 1;
                    $catalog->course_id = $course->id;
                    $catalog->title = $faker->word;
                    $catalog->parent_id = "0";
                    $catalog->created_at = $faker->unixTime;
                    $catalog->updated_at = $faker->unixTime;

                    if ($catalog->save()) {

                        for ($sct = 0; $sct < 10; $sct++) {
                            $subcatalog = new CourseCatalog();
                            $subcatalog->id = Utils::uniqueKey();
                            $subcatalog->user_id = 1;
                            $subcatalog->course_id = $course->id;
                            $subcatalog->title = $faker->word;
                            $subcatalog->parent_id = $catalog->id;
                            $subcatalog->created_at = $faker->unixTime;
                            $subcatalog->updated_at = $faker->unixTime;

                            if ($subcatalog->save()) {

                                for ($con = 0; $con < 10; $con++) {

                                    //创建课程封面
                                    $cnfile = new File();
                                    $cnfile->id = Utils::uniqueKey();
                                    $cnfile->user_id = 1;
                                    $cnfile->filename = $faker->word;
                                    $cnfile->size = $faker->numberBetween(1000, 9999) . "";
                                    $cnfile->hash = $faker->sha1;
                                    $cnfile->type = 'video/mp4';
                                    $cnfile->ext = 'mp4';
                                    $cnfile->server = $faker->imageUrl(476, 270);
                                    $cnfile->created_at = $faker->unixTime;
                                    $cnfile->updated_at = $faker->unixTime;

                                    if (!$cnfile->save()) {
                                        var_dump($cnfile->errors);
                                        break;
                                    }

                                    //'id','user_id','title','course_id','course_catalog_id','file_id'
                                    $content = new CourseContent();
                                    $content->id = Utils::uniqueKey();
                                    $content->user_id = 1;
                                    $content->course_id = $course->id;
                                    $content->course_catalog_id = $subcatalog->id;
                                    $content->title = $faker->word;
                                    $content->file_id = $cnfile->id;
                                    $content->created_at = $faker->unixTime;
                                    $content->updated_at = $faker->unixTime;

                                    if (!$content->save()) {
                                        var_dump($content->errors);
                                        break;
                                    }
                                }
                            } else {
                                print_r($subcatalog->errors);
                                break;
                            }
                        }
                    } else {
                        print_r($catalog->errors);
                        break;
                    }
                }
            } else {
                print_r($course->errors);
                break;
            }
        }
        echo "添加" . $a . "条数据";
    }

}